﻿//给QMS检验实测值录入数据
//Copyright (c) : 通力凯顿（北京）系统集成有限公司
//Writer:Wangjh
//create Date:2022-12-07
//Rewriter:
//Rewrite Date:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using UFC.Common;
using UFC.DBUtility;
using System.Data;
using System.Text.RegularExpressions;
using System.Collections;
using System.Data.OracleClient;

namespace MESQualityHistoryRecord
{
    class QMSCheckItemResultInfo
    {

        public void Excute() {
            //机加检验检测项数据处理
            System.Threading.Thread thJJCheckItemResult = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
            {
                try
                {
                    InsertCheckItemData();
                }
                catch (Exception e)
                {
                    UFC.Common.LogHelp.WriteError("机加检验检测项数据处理", e);
                }
            }));
            thJJCheckItemResult.Start();








            //线程等待区域
            //机加检验检测项数据处理
            do
            {
                System.Threading.Thread.Sleep(500);
            } while (thJJCheckItemResult.ThreadState != System.Threading.ThreadState.Stopped);

         
        }
         
        #region 机加检验实测值数据结转 add:Wangjh 20221217
        /// <summary>
        /// 获取检验后有实测值的记录（中间表无值）
        /// </summary>
        /// <returns></returns>
        DataTable GetCheckItemResultData()
        {
            string strsql = @"select tr.id,SR.Childcontainerid,c.mfgordername,c.containername,'' BigSpec,'' EngineNo,c.productname,c.realcontainerno ProductBatchNo,
'WF-'||w.process_no||':'||w.workflowrevision WorkflowBaseName,0 BillType,substr(ws.workflowstepname,0,instr(ws.workflowstepname,'-')-1) SpecNO,
substr(ws.workflowstepname,instr(ws.workflowstepname,'-')+1) SpecDesc,cci.acceptanceqty CheckQualifiedQty,st.checkno,st.name CheckPointName,
st.checkpointnotedis_b CheckPointNote,st.drawingrequiredis_b DrawingRequire,st.checktype,st.KeyCheck,st.MeopCheck,
st.QualityCheck,sr.checkpointresultnotedis_b CheckPointResultNote,e.fullname CheckerName,to_char(sr.checkdate,'yyyy/mm/dd hh24:mi:ss') CheckDate,
sr.workcheckpointresultnotedis_b WorkCheckPointResultNote,e2.fullname WorkerName,to_char(sr.createdate,'yyyy/mm/dd hh24:mi:ss') WorkerCheckDate,
cci.containercheckinfoid,sr.containerstepworkreportid,sr.stepcheckpointresultid,st.IsInstall,st.IsDisassemble,st.IsNotice,st.IsReturn,st.IsCompp  
from containercurrentinfo c
left join stepcheckpointresult sr on c.containerid=sr.containerid AND sr.checkdate >=sysdate-5
left join stepcheckpoint st on st.stepcheckpointid=sr.stepcheckpointid
left join containerstepworkreport csw on csw.containerstepworkreportid=sr.containerstepworkreportid and csw.isuse=0
inner join containercheckinfo cci on cci.containerstepworkreportid=csw.containerstepworkreportid and cci.isuse=0
left join workflowstep ws on ws.workflowstepid=csw.workflowstepid
left join workflow w on w.workflowid=ws.workflowid
left join T_CheckItemResultInfo tr on tr.stepcheckpointresultid=sr.stepcheckpointresultid
left join employee e on e.employeeid=sr.checkerid
left join employee e2 on e2.employeeid=sr.workerid
where NVL2(sr.STEPCHECKPOINTRESULTID,'1','0')='1' and c.factoryname not like '发动机%' and ( 
NVL2(tr.id,'1','0')='0' 
OR dbms_lob.compare( tr.CheckPointResultNote,sr.checkpointresultnotedis_b)<>0
OR (sr.checkpointresultnotedis_b IS NOT NULL AND tr.checkpointresultnote IS NULL)
)  
and rownum<1001";
            return OracleHelper.Query (strsql).Tables[0];
        }

        /// <summary>
        /// 处理新增或增加数据
        /// </summary>
        void InsertCheckItemData()
        {
            DataTable dt = GetCheckItemResultData();
            List<object[]> SQLStringList = new List<object[]>();
            //新增5个字段：IsInstall,IsDisassemble,IsNotice,IsReturn,IsCompp add by longb 2023-12-26
            var inertSql = @"insert into T_CheckItemResultInfo t (t.id,CreateDate,MfgOrderName,ContainerName,BigSpec,EngineNo,ProductName,ProductBatchNo,
WorkflowBaseName,BillType,SpecNO,SpecDesc,CheckQualifiedQty,CheckNO,CheckPointName,CheckPointNote,DrawingRequire,CheckType,
KeyCheck,MeopCheck,QualityCheck,CheckPointResultNote,CheckerName,CheckDate,WorkCheckPointResultNote,WorkerName,WorkerCheckDate,
ContainerCheckInfoID,ContainerStepWorkReportID,stepcheckpointresultID,status,IsInstall,IsDisassemble,IsNotice,IsReturn,IsCompp) values (sys_guid(),sysdate,'{0}','{1}','{2}','{3}','{4}',
'{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}',{13},{14},'{15}','{16}','{17}','{18}',{19},'{20}','{21}',{22},'{23}',
'{24}','{25}','{26}','{27}',0,'{28}','{29}','{30}','{31}','{32}')";
            var updateSql = @"update T_CheckItemResultInfo tr set tr.CheckPointResultNote={0},tr.CheckerName='{1}',tr.CheckDate='{2}',tr.WorkerName='{3}',
tr.WorkerCheckDate='{4}',tr.WorkCheckPointResultNote={5},CheckQualifiedQty='{6}',ContainerStepWorkReportID='{7}',
ContainerCheckInfoID='{8}',tr.status=0 where tr.id='{9}'";
            foreach (DataRow dr in dt.Rows)
            {
                if (string.IsNullOrWhiteSpace(dr["id"].ToString()))
                {
                    OracleParameter[] param = new OracleParameter[4];

                    param[0] = new OracleParameter("CheckPointNote", OracleType.Clob);
                    param[0].Direction = ParameterDirection.Input;
                    param[0].Value = dr["CheckPointNote"].ToString ();

                    param[1] = new OracleParameter("DrawingRequire", OracleType.Clob);
                    param[1].Direction = ParameterDirection.Input;
                    param[1].Value = dr["DrawingRequire"].ToString();

                    param[2] = new OracleParameter("CheckPointResultNote", OracleType.Clob);
                    param[2].Direction = ParameterDirection.Input;
                    param[2].Value = dr["CheckPointResultNote"].ToString();

                    param[3] = new OracleParameter("WorkCheckPointResultNote", OracleType.Clob);
                    param[3].Direction = ParameterDirection.Input;
                    param[3].Value = dr["WorkCheckPointResultNote"].ToString();

                    SQLStringList.Add(new object[2] {string.Format(inertSql, dr["MfgOrderName"].ToString(), dr["ContainerName"].ToString(), dr["BigSpec"].ToString(), dr["EngineNo"].ToString(),
                        dr["ProductName"].ToString(), dr["ProductBatchNo"].ToString(), dr["WorkflowBaseName"].ToString(), dr["BillType"].ToString(), dr["SpecNO"].ToString(), dr["SpecDesc"].ToString(),
                        dr["CheckQualifiedQty"].ToString(), dr["CheckNO"].ToString(), dr["CheckPointName"].ToString(), ":CheckPointNote", ":DrawingRequire", dr["CheckType"].ToString(),
                        dr["KeyCheck"].ToString(), dr["MeopCheck"].ToString(), dr["QualityCheck"].ToString(), ":CheckPointResultNote", dr["CheckerName"].ToString(), dr["CheckDate"].ToString(),
                        ":WorkCheckPointResultNote", dr["WorkerName"].ToString(), dr["WorkerCheckDate"].ToString(), dr["ContainerCheckInfoID"].ToString(), dr["ContainerStepWorkReportID"].ToString(), 
                        dr["stepcheckpointresultID"].ToString(),dr["IsInstall"].ToString(),dr["IsDisassemble"].ToString(),dr["IsNotice"].ToString(),dr["IsReturn"].ToString(),dr["IsCompp"].ToString()),param});
                }
                else
                {
                    OracleParameter[] param = new OracleParameter[2];

                    param[0] = new OracleParameter("CheckPointResultNote", OracleType.Clob);
                    param[0].Direction = ParameterDirection.Input;
                    param[0].Value = dr["CheckPointResultNote"].ToString();

                    param[1] = new OracleParameter("WorkCheckPointResultNote", OracleType.Clob);
                    param[1].Direction = ParameterDirection.Input;
                    param[1].Value = dr["WorkCheckPointResultNote"].ToString();

                    SQLStringList.Add(new object[2] {string.Format(updateSql, ":CheckPointResultNote", dr["CheckerName"].ToString(), dr["CheckDate"].ToString(),
                        dr["WorkerName"].ToString(), dr["WorkerCheckDate"].ToString(), ":WorkCheckPointResultNote", dr["CheckQualifiedQty"].ToString(),
                        dr["ContainerStepWorkReportID"].ToString(), dr["ContainerCheckInfoID"].ToString(), dr["id"].ToString()),param});
                }
            }
            OracleHelper.ExecuteSqlTran(SQLStringList );
            UFC.Common.LogHelp.WriteLog("QMS中间表T_CheckItemResultInfo更新或插入数据条数：" + dt.Rows.Count.ToString());
        }

        /// <summary>
        /// 处理撤销导致的删除数据
        /// </summary>
        void DelCheckItemData() { 
        
        }
        #endregion
              

    }
}
